package com.xjh.him;

import com.xjh.him.netty.HandlerCallback;
import com.xjh.him.netty.HandlerListener;
import com.xjh.him.netty.NettyServer;
import com.xjh.him.netty.handler.BaseHandler;
import com.xjh.him.service.FriendGroupMsgServiceImpl;
import com.xjh.him.service.UserGroupMsgServiceImpl;
import com.xjh.him.service.UserServiceImpl;
import com.xjh.him.util.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.Arrays;

@Slf4j
@SpringBootApplication
@MapperScan("com.xjh.him.dao")
public class HimSev implements InitializingBean {

    private static int NETTY_PORT;

    @Value("${netty.port}")
    private int nettyPort;

    public static void main(String[] args) {
        SpringApplication.run(HimSev.class);
        startNetty();
        subscribeCallback();
    }

    public static void startNetty() {
        NettyServer server = new NettyServer(NETTY_PORT, Arrays.asList(new BaseHandler()));
        server.start();
    }

    public static void subscribeCallback() {
        HandlerCallback.subscribe(SpringUtil.getBean(UserServiceImpl.class));
        HandlerCallback.subscribe(SpringUtil.getBean(FriendGroupMsgServiceImpl.class));
        HandlerCallback.subscribe(SpringUtil.getBean(UserGroupMsgServiceImpl.class));

        for (HandlerListener listener : HandlerCallback.listenerList) {
            log.info("HandlerListener:{} register", listener.getClass().getName());
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        NETTY_PORT = this.nettyPort;
    }
}
